package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.exception.CheckGroupException;
import com.itheima.exception.CheckItemException;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: libin
 * @date: 2019/11/20 21:04
 * @version: 1.0
 * @describe:
 */
@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;

    /**
     * 分页查询检查组
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findAll")
    public PageResult findAll(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = checkGroupService.findAll(
               queryPageBean.getCurrentPage(),
               queryPageBean.getPageSize(),
               queryPageBean.getQueryString()
       );

        return pageResult;
    }


    /**
     * 新增检查组
     * @param checkGroup
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup) {
        try {
            checkGroupService.add(checkGroup);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }

        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    /**
     * 查询检查组的所有信息
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(@RequestParam("id") Integer id) {
        CheckGroup checkGroup = null;
        try {
            checkGroup = checkGroupService.findById(id);
        } catch (Exception e) {
            return new Result(false,"数据更新失败，请刷新！") ;
        }
        //查询的数据一定要放到Result中
        return new Result(true,"",checkGroup);

    }

    /**
     * 编辑检查组
     * @param checkGroup
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup) {

        try {
            checkGroupService.edit(checkGroup);
            //成功
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            //失败
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }

    }

    /**
     * 删除检查组
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(@RequestParam("id") Integer id) {
        try {
            checkGroupService.delete(id);
        } catch (CheckGroupException e) {
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    /**
     * 查询所有检查项
     * @return
     */
    @RequestMapping("/findAllCheckGroup")
    public Result findAllCheckGroup() {
        List<CheckGroup> CheckGroupList = checkGroupService.findAllCheckGroup();
        if (CheckGroupList != null && CheckGroupList.size() > 0) {
            //查询成功
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, CheckGroupList);
        } else {

            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
}
